from rest_framework.generics import ListAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
import django.db.models as models 
from .models import Product
from .serializers import ProductSerializer
from rest_framework.pagination import PageNumberPagination
from rest_framework import status
from rest_framework.response import Response


class CustomPagination(PageNumberPagination):
    page_size = 8
    page_size_query_param = 'page_size'
    max_page_size = 100

class ProductListAPIView(ListAPIView):
    serializer_class = ProductSerializer
    pagination_class = CustomPagination
    def get_queryset(self):
        # 获取原始查询集
        queryset = Product.objects.all()
        
        # 从请求参数中获取搜索关键词
        search = self.request.query_params.get('search', None)
        
        # 如果有搜索关键词，根据商品名称和描述进行过滤
        if search:
            queryset = queryset.filter(
                models.Q(name__icontains=search) | 
                models.Q(description__icontains=search)
            )
            
        return queryset


# 添加商品创建视图
class ProductCreateAPIView(CreateAPIView):
    serializer_class = ProductSerializer
    
    def perform_create(self, serializer):
        # 保存商品信息
        serializer.save()
    
    def create(self, request, *args, **kwargs):
        try:
            response = super().create(request, *args, **kwargs)
            return Response({
                'code': 0,
                'message': '商品添加成功',
                'data': response.data
            }, status=status.HTTP_201_CREATED)
        except Exception as e:
            return Response({
                'code': 1,
                'message': f'添加商品失败: {str(e)}',
                'data': None
            }, status=status.HTTP_400_BAD_REQUEST)

# 更新商品视图
class ProductUpdateAPIView(UpdateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    lookup_field = 'pk'  # 使用主键作为查询参数
    
    def perform_update(self, serializer):
        # 保存更新后的商品信息
        serializer.save()
    
    def update(self, request, *args, **kwargs):
        try:
            response = super().update(request, *args, **kwargs)
            return Response({
                'code': 0,
                'message': '商品更新成功',
                'data': response.data
            }, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                'code': 1,
                'message': f'商品更新失败: {str(e)}',
                'data': None
            }, status=status.HTTP_400_BAD_REQUEST)

# 删除商品视图
class ProductDeleteAPIView(DestroyAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    lookup_field = 'pk'  # 使用主键作为查询参数
    
    def destroy(self, request, *args, **kwargs):
        try:
            super().destroy(request, *args, **kwargs)
            return Response({
                'code': 0,
                'message': '商品删除成功',
                'data': None
            }, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                'code': 1,
                'message': f'商品删除失败: {str(e)}',
                'data': None
            }, status=status.HTTP_400_BAD_REQUEST)
